﻿using HCQ2_Common;
using HCQ2_Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HCQ2_Model.ViewModel;
using HCQ2_Model.VRDevice.ParamModel;
using HCQ2_Model.VRDevice.ResultModel;
using System.Web;
using System.IO;
using System.Diagnostics;
using HCQ2_Model.VRDD.OutputDto._211217;
using HCQ2_Model.VRQDMeta.OutputDto;
using HCQ2_Model.DocModel.APIParam;

namespace HCQ2UI_Logic.DocumentController
{
    /// <summary>
    ///  奇点元宇宙管理
    /// </summary>
    public class VRQDMetaController : BaseLogic
    {
        //**************************1.0 奇点元宇宙【应用】*****************************
        #region 1.0 初始化Table + ActionResult InitProgramTable()
        /// <summary>
        ///  初始化Table
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult InitProgramTable(QDMetaParam model)
        {
            if (model.folder_id == 0)
                return null;
            int total;
            model.user_id = operateContext.Usr.user_id;
            List<MetaListResult> list = operateContext.bllSession.T_DocumentInfo.GetQDMetaList(model,out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = list
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 1.1 编辑元宇宙【应用】 + ActionResult EditProgram(DocumentModel param)
        /// <summary>
        ///  编辑元宇宙【应用】
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditProgram(DocumentModel file)
        {
            int folder_id = RequestHelper.GetIntByName("folder_id");
            string folder_path = RequestHelper.GetStrByName("folder_path"),
                isApprove = RequestHelper.GetStrByName("isApprove");//是否审核
            if (file.file_id <= 0)
                return operateContext.RedirectAjax(1,"关键字为空！", "", "");
            //判断课件代码
            if (string.IsNullOrEmpty(file.file_code))
                return operateContext.RedirectAjax(1, "代码必填字段不允许为空~", "", "");
            int fileTemp = 0;
            fileTemp = operateContext.bllSession.T_DocumentInfo.SelectCount(s => s.file_code.Equals(file.file_code, StringComparison.CurrentCultureIgnoreCase) && s.file_id != file.file_id && s.file_status == 5);
            if (fileTemp > 0)
                return operateContext.RedirectAjax(1, "当前课件代码已成功上架、请重新编辑代码 或者 下架同代码的课件！注意：代码不区分大小写~", "", "");
            //最低价格
            if (file.listPrice != null && file.listPrice.Count > 0)
                file.file_money = file.listPrice.OrderBy(s => s.file_price).FirstOrDefault().file_price;
            else
                file.file_money = 0;
            file.update_name = operateContext.Usr.user_name;
            file.update_time = DateTime.Now;
            string[] temp = { "file_name", "file_note", "pag_num", "finish_time", "is_recommend", "file_code", "file_order", "attach_url", "file_size", "browse_num", "vr_star", "pay_num", "down_number", "update_time", "update_name", "file_money" };
            List<string> modifyStr = new List<string>(temp);
            //是否审核
            if (!string.IsNullOrEmpty(isApprove) && isApprove.Equals("approve"))
            {
                file.approve_time = DateTime.Now;
                file.approve_name= operateContext.Usr.user_name;
                modifyStr.Add("approve_time");
                modifyStr.Add("approve_name");
                modifyStr.Add("file_status");
                modifyStr.Add("approve_note");
            }
            int mark = operateContext.bllSession.T_DocumentInfo.Modify(file, s => s.file_id == file.file_id, modifyStr.ToArray());
            if (mark > 0)
            {
                T_DocumentFolderRelation rel = operateContext.bllSession.T_DocumentFolderRelation.Select(s => s.file_id == file.file_id).FirstOrDefault();
                if (rel != null)
                    operateContext.bllSession.T_DocumentFolderRelation.Modify(new T_DocumentFolderRelation { folder_id = folder_id, folder_path = folder_path }, s => s.fr_id == rel.fr_id, "folder_id", "folder_path");
                else
                    operateContext.bllSession.T_DocumentFolderRelation.Add(new T_DocumentFolderRelation { file_id = file.file_id, create_id = file.create_id, folder_id = folder_id, folder_path = folder_path });
                //更新金额
                operateContext.bllSession.T_VRGoodDisiPrice.Delete(s => s.file_id == file.file_id);
                if (file.listPrice != null && file.listPrice.Count > 0)
                {
                    file.listPrice.ForEach(o => operateContext.bllSession.T_VRGoodDisiPrice.Add(new T_VRGoodDisiPrice
                    {
                        op_code = Helper.GetGuid,
                        file_id = file.file_id,
                        file_code = file.file_code,
                        price_name= o.op_month>0? o.op_month.ToString()+ "个月价格": "无限期价格",
                        op_month = o.op_month,
                        ori_price = o.file_price
                    }));
                }                
                return operateContext.RedirectAjax(0, "数据更新成功~", "", "");
            }
            return operateContext.RedirectAjax(1, "数据更新失败！", "", "");         
        }
        #endregion

        #region 1.2 批量转移目录 + ActionResult SaveBatchData()
        /// <summary>
        ///  批量转移目录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveBatchData()
        {
            int folder_id = RequestHelper.GetIntByName("folder_id");//导航id
            string fileIds = RequestHelper.GetStrByName("fileIds"),//作品id集合
                folder_path = RequestHelper.GetStrByName("folder_path");//导航path
            if (string.IsNullOrEmpty(fileIds))
                return operateContext.RedirectAjax(1, "待修改的作品id为空！", "", "");
            if (folder_id <= 0 || string.IsNullOrEmpty(folder_path))
                return operateContext.RedirectAjax(1, "导航目录id异常！", "", "");
            try
            {
                string[] fileID = fileIds.Split(',');
                T_DocumentFolderRelation folderRel = new T_DocumentFolderRelation { folder_id = folder_id, folder_path = folder_path };
                //编辑导航目录
                foreach (string item in fileID)
                {
                    int file_id = Helper.ToInt(item);
                    operateContext.bllSession.T_DocumentFolderRelation.Modify(folderRel, s => s.file_id == file_id, "folder_id", "folder_path");
                }
                return operateContext.RedirectAjax(0, "数据更新成功~", "", "");
            }
            catch (Exception ex)
            {
                return operateContext.RedirectAjax(1, ex.Message, "", "");
            }
        }
        #endregion

        #region 1.3 获取数据 + ActionResult GetProgramData()
        /// <summary>
        ///  获取数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetProgramData(int id)
        {
            if (id <= 0)
                return null;
            var obj = operateContext.bllSession.T_DocumentInfo.GetProgramData(id);
            return operateContext.RedirectAjax(0, "获取数据成功~", obj, "");
        }
        #endregion

        //**************************2.0 奇点元宇宙【房间】*****************************
        #region 1.0 初始化Table + ActionResult InitRoomTable()
        /// <summary>
        ///  初始化Table
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult InitRoomTable(QDMetaParam model)
        {
            if (model.folder_id == 0)
                return null;
            int total;
            model.user_id = operateContext.Usr.user_id;
            List<MetaRoomResult> list = operateContext.bllSession.T_DocumentInfo.GetQDMetaRoomList(model, out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = list
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 2.1 编辑元宇宙【房间】 + ActionResult EditRoom(DocumentModel param)
        /// <summary>
        ///  编辑元宇宙【房间】
        /// </summary>
        /// <param name="file">用户对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditRoom(DocumentModel file)
        {
            string isApprove = RequestHelper.GetStrByName("isApprove");//是否审核
            if (file.file_id <= 0)
                return operateContext.RedirectAjax(1, "关键字为空！", "", "");
            //判断课件代码
            if (string.IsNullOrEmpty(file.file_code))
                return operateContext.RedirectAjax(1, "代码必填字段不允许为空~", "", "");
            int fileTemp = 0;
            fileTemp = operateContext.bllSession.T_DocumentInfo.SelectCount(s => s.file_code.Equals(file.file_code, StringComparison.CurrentCultureIgnoreCase) && s.file_id != file.file_id && s.file_status == 5);
            if (fileTemp > 0)
                return operateContext.RedirectAjax(1, "当前课件代码已成功上架、请重新编辑代码 或者 下架同代码的课件！注意：代码不区分大小写~", "", "");
            file.update_name = operateContext.Usr.user_name;
            file.update_time = DateTime.Now;
            string[] temp = { "file_name", "file_note", "pag_num", "qr_code", "is_recommend", "file_code", "file_order", "attach_url", "gps_x", "gps_y", "doc_number", "browse_num", "vr_star", "pay_num", "down_number", "update_time", "update_name", "file_money", "game_start", "game_end" };
            List<string> modifyStr = new List<string>(temp);
            //是否审核
            if (!string.IsNullOrEmpty(isApprove))
            {
                file.approve_time = DateTime.Now;
                file.approve_name = operateContext.Usr.user_name;
                modifyStr.Add("approve_time");
                modifyStr.Add("approve_name");
                modifyStr.Add("file_status");
                modifyStr.Add("approve_note");
            }
            int mark = operateContext.bllSession.T_DocumentInfo.Modify(file, s => s.file_id == file.file_id, modifyStr.ToArray());
            if (mark > 0)
            {
                //更新金额
                operateContext.bllSession.T_VRGoodDisiPrice.Delete(s => s.file_id == file.file_id);
                if(file.file_money>0)
                    operateContext.bllSession.T_VRGoodDisiPrice.Add(new T_VRGoodDisiPrice {
                        op_code = Helper.GetGuid,
                        file_id = file.file_id,
                        file_code = file.file_code,
                        op_month = 0,
                        ori_price = file.file_money
                    });
                return operateContext.RedirectAjax(0, "数据更新成功~", "", "");
            }
            return operateContext.RedirectAjax(1, "数据更新失败！", "", "");
        }
        #endregion
    }
}